﻿//  -------------------- iSharePoint -------------------- //
// 
//  Company:	IT Complex, LLC
//  Project:	Smart.Workflow
//  File name:	WorkflowRuleChecker.cs
//  Developer:	Solomatov Igor
//  Created:	16.04.2012
// 
//  -------------------- iSharePoint -------------------- //

using Smart.Common.Rules;

namespace Smart.Workflow.Rules
{
	public class WorkflowRuleChecker : XmlRuleChecker
	{
		public WorkflowRuleChecker(RulesCtx ctx)
			: base(ctx)
		{
		}

		public override bool ExecuteFunction(XmlRuleConditionFunction function, out bool handled)
		{
			// Check function in base classes
			var res = base.ExecuteFunction(function, out handled);

			if (handled)
				return res;

			// Check action rule function
			var ari = Ctx.Instance as WorkflowActionRulesInstance;

			if (ari == null)
				return false;

			res = ari.Service.ExecuteFunction(function.Name, function.Parameters, out handled);

			if (handled)
				return res;

			if (ari.Instance != null)
				res = ari.Instance.ExecuteFunction(function.Name, function.Parameters, out handled);

			if (handled)
				return res;

			return false;
		}
	}
}